<?php

class nbwiStatement
{
	const FETCH_ARRAY	= 1;
	const FETCH_ASSOC	= 2;
	const FETCH_OBJECT	= 3;
	
	private
		$connection,
		$resource;
	
	public function __construct($connection, $mysqlResource)
	{
		if (!is_resource($mysqlResource))
		{
			throw new nbwiException(
				'Argument passed to nbwiStatement must be a mysql resource',
				nbwiException::DATABASE_ERROR
			);
		}
		
		$this->connection	= $connection;
		$this->resource		= $mysqlResource;
	}
	
	public function affectedRows()
	{
		return mysql_affected_rows($this->connection);
	}
	
	public function getFetchMethod($fetchMethod)
	{
		switch ($fetchMethod)
		{
			case self::FETCH_ARRAY:
				$func = 'fetchArray';
				break;
			
			case self::FETCH_ASSOC:
				$func = 'fetchAssoc';
				break;
			
			case self::FETCH_OBJECT:
				$func = 'fetchObject';
				break;
		}
		
		return $func;
	}
	
	public function fetch($fetchMethod = self::FETCH_ASSOC)
	{
		return $this->{$this->getFetchMethod($fetchMethod)}();
	}
	
	public function fetchAll()
	{
		$array = array();
		
		while ($row = $this->fetchAssoc())
		{
			$array[] = $row;
		}
		
		return $array;
	}
	
	public function fetchArray()
	{
		return mysql_fetch_array($this->resource);
	}
	
	public function fetchAssoc()
	{
		return mysql_fetch_assoc($this->resource);
	}
	
	public function fetchObject()
	{
		return mysql_fetch_object($this->resource);
	}
	
	public function numRows()
	{
		return mysql_num_rows($this->resource);
	}
}

?>